<?php
namespace app\admin\controller;
use think\Db;
class Express extends SessionCheck
{
    public function index()
    {
        $aid = input('get.aid');
        $where['isExpress'] = 1;//认证过的可以显示
        if (!empty($aid)) {
            $where['aid'] = $aid;
        }
        $pages = pages('user', $where, input('get.page'));
        $data = Db::table("user")->where($where)->whereIn('aid', session('aid'))->limit($pages['limit'])->order('id desc')->select();
        foreach ($data as $k => $v) {
         $data[$k]['express_type_name'] = Db::table('express_type')->where('id',$v['express_type'])->value('name');//职业类型
        }
        return view('', ['data' => $data, 'pages' => $pages, 'menu_list' => $this->menu_list]);
    }
    //
    public function edit()
    {
         $id = input("get.id") ? input("get.id") : 0;
         if(request()->isPost()){
            $data = input("post.");
            $cate=[];
            if($id){
                Db::table("user")->where(['id' => $id])->update($data);
                Db::table("express_check")->where('uid',$id)->update(["idcard"=>$data['idcard'],"ali_number"=>$data['ali_number'],'true_name'=>$data['true_name']]);
                $data['id'] = $id;
            } else{
                if(Db::table("user")->where(['phone' => $data['phone']])->where('id', '<>', $id)->find()) {
                    return error(lang('tip')['re_phone']);
                }
                $data['business'] = 1;//正常接单状态
                $data['isExpress']=1;//直接认证
                $data['id'] = Db::table("user")->insertGetId($data);//酒店认证之后往express_check里填充
                $id = $data['id'];
                $cate['pid']=$data['pid'];
                $cate['cid']=$data['cid'];
                $cate['aid']=$data['aid'];
                $cate['uid']=$id;
                $cate['true_name']=$data["true_name"];
                $cate['true_head']=$data['true_head'];
                $cate['idcard']=$data['idcard'];
                $cate['idcard_front']=$data['idcard_front'];
                $cate['ali_number']=$data['ali_number'];
                $cate['status']=1;//正常状态
                $cate['express_type']=2;//酒店类型
                $cate['retime']=time();
                Db::table("express_check")->insert($cate);
            }
        }
        $data = Db::table("user")->where(['id' => $id])->find();
        $express_type_arr = Db::table('express_type')->select();
        return view('', ['data' => $data, 'express_type_arr'=>$express_type_arr,'menu_list' => $this->menu_list]);
    }

    public function del()
    {
        $id = input("get.id") ? input("get.id") : 0;
        $page = input("get.page") ? input("get.page") : 1;
        Db::table('user')->whereIn('aid', session('aid'))->whereIn('id', $id)->update(['isExpress' => 0]);
        $uid_arr = explode(',',$id);
        $push_array = [];
        foreach ($uid_arr as $v){
            if(!empty($v)){
                $push_array[] = md5($v);
                insert_message($v,'系统通知','根据最近时间的系统检测，发现您不能胜任跑客，已给您撤销此职务','2');
            }
        }
        Db::table('express_check')->whereIn('uid',$id)->where('status',1)->update(['status'=>3]);
        push($push_array,json_encode(['type'=>1,'message'=>'根据最近时间的系统检测，发现您不能胜任司机，已给您撤销此职务','parameters'=>'','status'=>2]));
        $this->redirect(url('/admin/express') . '?page=' . $page);
        exit;
    }

    public function setting()
    {
        $tab = request()->request('tab');
        $aid = request()->request('aid');
        $data = [];
        $where = [];
        if (!empty($aid)) {
            $where['aid'] = $aid;
            $count = Db::table('express_config')->where($where)->count();
            if ($count == 0) {
                $insert['pid'] = request()->request('pid');
                $insert['cid'] = request()->request('cid');
                $insert['aid'] = $aid;
                $insert['scope'] = 2;
                $insert['basics_charge'] = 2;
                $insert['white_charge'] = 4;
                $insert['night_charge'] = 6;
                $insert['weight_charge'] = 3;
                $insert['reward_integral'] = 3;
                $insert['anxious_charge'] = 3;
                $insert['fast_charge'] = 4;
                $insert['express_icon'] = 'http://'.$_SERVER['HTTP_HOST'].'/express.png';
                $insert['icon_status'] = 0;
                Db::table('express_config')->insert($insert);
            }
        }
        if (!empty($tab)){
            $post = request()->post();
            if(!array_key_exists('icon_status',$post)){
                $post['icon_status'] = 0;
            }
            Db::table('express_config')->whereIn('aid', session('aid'))->where($where)->update($post);
            $this->redirect(url('express/setting').'?pid='.request()->get('pid').'&cid='.request()->get('cid').'&aid='.request()->get('aid'));
            exit;
        }
        if (!empty($aid)) {
            $data = Db::table('express_config')->whereIn('aid', session('aid'))->where($where)->find();
        }
        return view('', ['data' => $data, 'menu_list' => $this->menu_list]);
    }

    public function losemoney()
    {
        $where = [];
        $aid = request()->request('aid');
        if($aid){
            $where['aid'] = $aid;
        }
        $pages = pages('losemoney', $where, input('get.page'));
        $data = Db::table("losemoney")->whereIn('aid', session('aid'))->where($where)->limit($pages['limit'])->order('id desc')->select();
        foreach ($data as $k => $v){
            $data[$k]['username'] = Db::table('user')->where('id', $v['uid'])->value('name');
        }
        return view('', ['data' => $data, 'pages' => $pages['pages'], 'menu_list' => $this->menu_list]);
    }

    public function losemoney_edit()
    {
        $id = request()->request('id');//[losemoney: id]
        $page = input("get.page");
        $uid = request()->request('uid');
        //$phone = Db::table('user')->where('id', $uid)->value('phone');
        Db::table('losemoney')->where('id', $id)->update(['status' => 1]);
        $money = Db::table('losemoney')->where('id', $id)->find();
        Db::table('user')->where('id', $uid)->setDec('losemoney',$money['money']);
        
        push(md5($uid), json_encode(['type' => 2, 'message' => '您的提现已成功打款，请注意查收', 'parameters'=>'','status'=>1]));
        insert_message($uid,'提现记录','您的提现已成功打款，请注意查收',2);

        $this->redirect(url('user/losemoney') . '?page=' . $page);
        exit;
    }

    public function losemoney_del()
    {
        $id = input("get.id") ? input("get.id") : 0;
        $page = input("get.page");
        $uid = request()->request('uid');
        $user = Db::table('user')->where('id', $uid)->find();
        $money = $user['money'] + $user['losemoney']/(1-$user['express_cut']/100);//拒绝后返还抽成金额
        $system_money=$user['losemoney']/(1-$user['express_cut']/100)*($user['express_cut']/100);//系统抽成的价格
        Db::table("system_money")->where(["type"=>1,'id'=>1])->setDec("system_money",$system_money);//总平台撤回本次抽成
        $system=Db::table("system_money")->where('type',2)->select();//地方上所有的情况
        foreach($system as $v){
         $system_id=$v["id"];
         $system_aid=$v['aid'];
         Db::table("system_money")->where(['id'=>$system_id,'aid'=>$system_aid])->setDec("system_money",$system_money);//地方撤回本次抽成 
        }
        $losemoney = 0;
        Db::table("user")->where('id', $user['id'])->update(['money' => $money, 'losemoney' => $losemoney]);
        push(md5($uid), json_encode(['type' => 2, 'message' => '您的提现由于某种原因被拒绝', 'parameters'=>'','status'=>1]));
        insert_message($uid,'提现记录','您的提现由于某种原因被拒绝',2);
        Db::table("losemoney")->where('id', $id)->update(['status' => 3]);
        $this->redirect(url('express/losemoney') . '?page=' . $page);
        exit;
    }
     
    public function set()
    {
        $pid = request()->request('pid');
        $cid = request()->request('cid');
        $aid = request()->request('aid');
        $where = [];
        if (!empty($aid)) {
            $where['aid'] = $aid;
        }
        $data = Db::table('express_type')->whereIn('aid', session('aid'))->where($where)->select();
        return view('', ['data' => $data, 'menu_list' => $this->menu_list]);
    }

    public function set_edit()
    {
        $id = input("get.id") ? input("get.id") : 0;
        if (request()->isPost()) {
            $data = request()->post();
            if(empty($id)) {
                $id = Db::table('express_type')->insertGetId($data);
                Db::table('express_type_order')->insert(['express_type'=>$id]);
            } else {
                Db::table('express_type')->whereIn('aid',session('aid'))->where('id',$id)->update($data);
            }
        }
        $data = Db::table("express_type")->where(['id' => $id])->find();
        return view('', ['data' => $data, 'menu_list' => $this->menu_list]);
    }

    public function set_del()
    {
        $id = input('get.id')?input('get.id'):0;
        $page = input('get.page');
        Db::table('express_type')->whereIn('aid',session('aid'))->delete($id);
        $this->redirect(url('express/set').'?page='.$page);
        exit;
    }

    public function set_relevance()
    {
        $express_type = request()->request('id');
        $order_array = ['1'=>'叫个车','2'=>'跑个腿','3'=>'拼个车','4'=>'订个房'];
        if(request()->isPost()){
            $post = request()->post();
            
            if(array_key_exists('order_type',$post)){
                $order_type = $post['order_type'];
                $order_type = ','.implode(',',$order_type).',';
            }else{
                $order_type = '';
            }
            Db::table('express_type_order')->where('express_type',$express_type)->update(['order_type'=>$order_type]);
        }
        $data = Db::table('express_type_order')->where('express_type',$express_type)->value('order_type');
        return view('', ['data' => $data,'order_array'=>$order_array, 'menu_list' => $this->menu_list]);
    }
    
     /**系统核准各地佣金的收入列表
      * 1:总平台的 type=1
      * 2:各地的 type=2 
      * **/
     public function system_money_list(){
        
        
        
     }
    
}